package com.jiepos.mpos.common.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.entity.App_Custom_BankcardEntity;
import com.jiepos.mpos.core.dao.BaseDao;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 结算信息变更管理
 * @ProjectName:  [mpos] 
 * @Package com.jiepos.mpos.common.dao 
 * @author 张磊
 * @version 1.0
 * @since 2017年9月26日 下午5:03:34
 * @history（历次修订内容、修订人、修订时间等）
 * @UpdateRemark [说明本次修改内容]
 * @Copyright (c) 2017 江苏杰博实信息技术有限公司
 */
@Repository
public class SettleAccountDao extends BaseDao<App_Custom_BankcardEntity>{

	public Integer getAccountReviewNum(String sql){
		return jdbcTemplate.queryForInt(sql);
	}
	
	public String reviewAccount(String audit_comments, String bankcard_uuid, Map<String, Object> map, boolean isPass){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String customerUUid = null;
		try {
			conn = jdbcTemplate.getDataSource().getConnection();
			/*改为手动提交事务*/
			conn.setAutoCommit(false);
			/*查询卡片信息*/
			StringBuffer sql = new StringBuffer("SELECT * FROM T_APP_CUSTOM_BANKCARD WHERE AUDIT_ST = ? AND BANKCARD_UUID = ? FOR UPDATE");
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, SystemConstant.REVIEW_ST_0);
			ps.setString(2, bankcard_uuid);
			rs = ps.executeQuery();
			if(rs.next()){
				int i = 1;
				/*获取原卡片标识*/
				String oldCardUUid = rs.getString("CHANGE_BANKCARD_UUID");
				/*获取会员标识*/
				customerUUid = rs.getString("CUSTOM_UUID");
				if(StringUtils.isNotBlank(oldCardUUid) && isPass){
					/*原始卡片数据，置为删除*/
					String delSql = "UPDATE T_APP_CUSTOM_BANKCARD SET REC_ST = ?, LAST_OPER_IN = ?, REC_UPD_TS= ?, REC_UPD_USR_ID = ? WHERE BANKCARD_UUID = ?";
					ps = conn.prepareStatement(delSql);
					ps.setString(i++, SystemConstant.REC_ST_0);
					ps.setString(i++, SystemConstant.OPER_DEL);
					ps.setTimestamp(i++, new Timestamp(System.currentTimeMillis()));
					ps.setString(i++, String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
					ps.setString(i++, oldCardUUid);
					ps.executeUpdate();
					/*原始卡片图片，置为删除*/
					i = 1;
					delSql = "UPDATE T_APP_AFFIX_INF SET REC_ST = ?, AFFIX_TP = ?, CHILD_AFFIX_IDX= ?, LAST_OPER_IN = ?, REC_UPD_TS= ?, REC_UPD_USR_ID = ? WHERE AFFIX_IDX = ? AND AFFIX_TP = ? AND REC_ST = ?";
					ps = conn.prepareStatement(delSql);
					ps.setString(i++, SystemConstant.REC_ST_0);
					ps.setString(i++, SystemConstant.AFFIX_TP_02);
					ps.setString(i++, oldCardUUid);
					ps.setString(i++, SystemConstant.OPER_DEL);
					ps.setTimestamp(i++, new Timestamp(System.currentTimeMillis()));
					ps.setString(i++, String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
					ps.setString(i++, customerUUid);
					ps.setString(i++, SystemConstant.AFFIX_TP_02);
					ps.setString(i++, SystemConstant.REC_ST_1);
					ps.executeUpdate();
				}
				/*更新卡片数据*/
				i = 1;
				sql = new StringBuffer("UPDATE T_APP_CUSTOM_BANKCARD SET AUDIT_ST = ?, AUDIT_TS=?, AUDIT_DESC = ?, LAST_OPER_IN = ?, REC_UPD_TS= ?,REC_UPD_USR_ID = ? WHERE BANKCARD_UUID = ?");
				/*审核通过，更新状态*/
				ps = conn.prepareStatement(sql.toString());
				/*审核通过参数*/
				if(isPass){
					ps.setString(i++, SystemConstant.REVIEW_ST_1);
					ps.setTimestamp(i++, new Timestamp(System.currentTimeMillis()));
				}else{
					ps.setString(i++, SystemConstant.REVIEW_ST_2);
					ps.setTimestamp(i++, null);
				}
				ps.setString(i++, audit_comments);
				ps.setString(i++, SystemConstant.OPER_UPDATE);
				ps.setTimestamp(i++, new Timestamp(System.currentTimeMillis()));
				ps.setString(i++, String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
				ps.setString(i++, bankcard_uuid);
				ps.executeUpdate();
				/*更新图片信息*/
				if(isPass){
					i = 1;
					sql = new StringBuffer("UPDATE T_APP_AFFIX_INF SET REC_ST = ?, AFFIX_TP = ?, CHILD_AFFIX_IDX= ? WHERE AFFIX_IDX = ? AND AFFIX_TP = ? AND REC_ST = ?");
					ps = conn.prepareStatement(sql.toString());
					ps.setString(i++, SystemConstant.REC_ST_1);
					ps.setString(i++, SystemConstant.AFFIX_TP_02);
					ps.setString(i++, bankcard_uuid);
					ps.setString(i++, customerUUid);
					ps.setString(i++, SystemConstant.AFFIX_TP_03);
					ps.setString(i++, SystemConstant.REC_ST_1);
					ps.executeUpdate();
				}
				/*增加工作流记录*/
				i = 1;
				sql = new StringBuffer("INSERT INTO T_APP_WORKFLOW_TRACE("
				+ "WORKFLOW_UUID, "
				+ "LOGIN_NAME, "
				+ "SYSTEM_TYPE, "
				+ "SYSTEM_KEY, "
				+ "OLD_STATE, "
				+ "NEW_STATE, "
				+ "STATE_OPERATE, "
				+ "AUDIT_COMMENTS, "
				+ "REC_ST, "
				+ "LAST_OPER_IN, "
				+ "REC_UPD_USR_ID, "
				+ "REC_UPD_TS, "
				+ "REC_CRT_TS) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)");
				ps = conn.prepareStatement(sql.toString());
				ps.setString(i++, IDFactoryUtil.getUUID());
				ps.setString(i++, String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
				ps.setString(i++, "T_APP_CUSTOM_BANKCARD");
				ps.setString(i++, bankcard_uuid);
				ps.setString(i++, SystemConstant.WORKFLOW_NODE_0);
				if(isPass){
					ps.setString(i++, SystemConstant.WORKFLOW_NODE_1);
					ps.setString(i++, SystemConstant.STATE_CHANGE_1);
				}else{
					ps.setString(i++, SystemConstant.WORKFLOW_NODE_2);
					ps.setString(i++, SystemConstant.STATE_CHANGE_2);
				}
				ps.setString(i++, audit_comments);
				ps.setString(i++, SystemConstant.REC_ST_1);
				ps.setString(i++, SystemConstant.OPER_ADD);
				ps.setString(i++, String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
				ps.setTimestamp(i++, new Timestamp(System.currentTimeMillis()));
				ps.setTimestamp(i++, new Timestamp(System.currentTimeMillis()));
				ps.executeUpdate();
			}
			/*提交事务*/
			conn.commit();
			return customerUUid;
		} catch (Exception e) {
			e.printStackTrace();
			if(conn != null){
				try {
					conn.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		} finally{
			if(rs != null){
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(ps != null){
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(conn != null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
}
